﻿/************************************************************************

 This file is part of "10 Second Tim".

 "10 Second Tim" is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 "10 Second Tim" is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with "10 Second Tim".  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2013 Scott Bevin. all rights reserved

using LDE.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;

namespace LDE
{
	/// <summary>
	/// This is the main type for your game
	/// </summary>
	public class GameMain : Microsoft.Xna.Framework.Game
	{
		public GraphicsDeviceManager graphics;
		private GameScreenManager gsMgr;

		public GameScreenManager GameScreensMgr { get { return gsMgr; } }

		private SpriteBatch spriteBatch;
		public SavedSettingsHelper Settings = new SavedSettingsHelper( );
		private Song backgroundMusic;

		public InputManager InputManager = new InputManager( );

		public GameMain( )
		{
			graphics = new GraphicsDeviceManager( this );
		}

		/// <summary>
		/// Allows the game to perform any initialization it needs to before starting to run.
		/// This is where it can query for any required services and load any non-graphic
		/// related content.  Calling base.Initialize will enumerate through any components
		/// and initialize them as well.
		/// </summary>
		protected override void Initialize( )
		{
			// TODO: Add your initialization logic here
			gsMgr = new GameScreenManager( this );
			spriteBatch = new SpriteBatch( GraphicsDevice );

			InputManager.Initialise( );

			Settings.Load( );

			Content.RootDirectory = "Content";

#if !SILVERLIGHT
			graphics.PreferMultiSampling = Settings.Settings.MSAAOn;
#endif

			graphics.ApplyChanges( );

			this.IsMouseVisible = true;

			base.Initialize( );
		}

		/// <summary>
		/// LoadContent will be called once per game and is the place to load
		/// all of your content.
		/// </summary>
		protected override void LoadContent( )
		{
			DebugDraw.Get = new DebugDraw( this.Content.Load<SpriteFont>( "Fonts/DebugFont" ) );

			MediaPlayer.IsRepeating = true;

#if !SILVERLIGHT
			SoundEffect.MasterVolume = Settings.Settings.SfxOn ? 0.25f : 0.0f;
#endif
		}

		/// <summary>
		/// UnloadContent will be called once per game and is the place to unload
		/// all content.
		/// </summary>
		protected override void UnloadContent( )
		{
			// TODO: Unload any non ContentManager content here
		}

		/// <summary>s
		/// Allows the game to run logic such as updating the world,
		/// checking for collisions, gathering input, and playing audio.
		/// </summary>
		/// <param name="gameTime">Provides a snapshot of timing values.</param>
		protected override void Update( GameTime gameTime )
		{
			float dt = (float)( gameTime.ElapsedGameTime.Milliseconds * 0.001f );

			InputManager.Update( dt );
			gsMgr.Update( dt );

			base.Update( gameTime );
		}

		/// <summary>
		/// This is called when the game should draw itself.
		/// </summary>
		/// <param name="gameTime">Provides a snapshot of timing values.</param>
		protected override void Draw( GameTime gameTime )
		{
			GraphicsDevice.Clear( Color.White );
			gsMgr.Render( (float)( gameTime.ElapsedGameTime.Milliseconds * 0.001f ), this.GraphicsDevice );

			//DebugDraw.Get.Render( spriteBatch );

			base.Draw( gameTime );
		}
	}
}